﻿package com.sos.ui
{ 
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	public class EffectBox extends MovieClip 
    {
		public var basePlayer:PlayerClip;
		public var effectCount:Number;
		public var boxName:String;
		public var stayUp:Boolean = false;
		
		public var effectDetailBox:EffectDetailBox;
		var effectList:Dictionary = new Dictionary();
		var smallIconList:Dictionary = new Dictionary();
		var largeIconList:Dictionary = new Dictionary();
		
		var numXrowLarge:Number = 5;
		var numXrowSmall:Number = 3;
		
		
		
		public function EffectBox(pl:PlayerClip) {
			boxName = "EB_" + pl.playerName;
			effectCount = 0;
			basePlayer = pl;
			if (basePlayer.side == 'home')
				effectDetailBox.x = effectDetailBox.x + 350;
				
			
			effectDetailBox.visible = false;
			
			effectDescBoxLarge.visible = false;
			effectDescBoxLarge.backPanel.visible = false;
			effectDescBoxSmall.descBoxSmallBorder.visible = false;
			this.visible = false;
			effectDescBoxLarge.effectText.visible = false;
			//this.addEventListener(MouseEvent.ROLL_OVER,handleMouseListeners,false,0,true);
			//this.addEventListener(MouseEvent.ROLL_OUT,handleMouseListeners,false,0,true);
		}
		
		public function addEffect(effect:Effect):void {
			effectCount++;
			effectList[effect.effectId] = effect;
			
			//Logger.debug("UI > EffectBox > EFFECT ADDED: " + effect.effectId + " -> " + effect.effectName);
			//traceEffects();
			if (this.visible == false)
				this.visible = true;
			
			addIcon(effect);
			
		}
		
		public function addIcon(effect:Effect, effectOrd:Number = 0):void {
			
			if (effectOrd == 0)
				effectOrd = effectCount;
			
			var rowLarge:Number = (Math.ceil(effectOrd/numXrowLarge));
			var rowSmall:Number = (Math.ceil(effectOrd/numXrowSmall));
			// x = row-1
			var newIcon:EffectIcon = new EffectIcon(effect, this, 'large');
			this.effectDescBoxLarge.addChild(newIcon);
			
			newIcon.x = ((effectOrd - ((rowLarge-1)*numXrowLarge))-1) * (newIcon.width + 5);
			newIcon.y = (this.effectDescBoxLarge.height - ((newIcon.height + 5) * rowLarge)) + 10;
			//trace("added icon for " + effect.effectName + " with X = " + newIcon.x);
			largeIconList[effect.effectId] = newIcon;
			
			var newSmallIcon:EffectIcon = new EffectIcon(effect, this, 'small');
			this.effectDescBoxSmall.addChild(newSmallIcon);
			newSmallIcon.x = ((effectOrd - ((rowSmall-1)*numXrowSmall))-1) * (newSmallIcon.width);
			newSmallIcon.y = (this.effectDescBoxSmall.height - ((newSmallIcon.height + 5) * rowSmall));
			
			smallIconList[effect.effectId] = newSmallIcon;
		}
		
		public function clearIcons():void {
			for each (var eIcon:EffectIcon in largeIconList) {
				this.effectDescBoxLarge.removeChild(eIcon);
			}
			for each (eIcon in smallIconList) {
				this.effectDescBoxSmall.removeChild(eIcon);
			}
		}
		
		public function redrawIcons():void {
			var i:Number = 1;
			for each (var effect:Effect in effectList) {
				
				addIcon(effect, i);
				i++;
			}
		}
		
		public function removeEffect(effectId:String):void {
			//Logger.debug("UI > EffectBox > Removing effect: " + effectId);
			if (effectList[effectId]) {
				//Logger.debug("UI > EffectBox > EFFECT REMOVED: " + effectId + " -> " + effectList[effectId].effectName);
				/*
				effectList.splice(effectId,1);
				smallIconList.splice(effectId,1);
				largeIconList.splice(effectId,1);
				*/
				this.effectDescBoxLarge.removeChild(largeIconList[effectId]);
				this.effectDescBoxSmall.removeChild(smallIconList[effectId]);
				delete effectList[effectId];
				delete smallIconList[effectId];
				delete largeIconList[effectId];
				
				
				effectCount--;
				//traceEffects();
				if (effectCount < 1)
					this.visible = false;
			}
			else {
				//trace("EFFECT ", effectId, " NOT FOUND ON ", basePlayer.playerName);
			}
		}
		
		
		
		public function traceEffects() {
			trace("TRACING EFFECTS ON " + basePlayer.playerName);
			effectDescBoxLarge.effectText.text = "";
			for each (var effect:Effect in effectList) {
				trace("EFFECT -> " + effect.effectName);
				//effectDescBoxLarge.effectText.text += efName + "\n";
			}
			trace("END TRACE EFFECTS");
		}
		
		public function handleMouseListeners(e:MouseEvent) {
			//trace(e.type);
			if (effectCount > 0) {
				switch (e.type) {
					case "rollOver":
						basePlayer.rolloverEnabled = false;
						effectDescBoxLarge.visible = true;
						
						//trace("ROLLOVER EFFECTBOX");
						break;
					case "rollOut":
						basePlayer.rolloverEnabled = true;
						effectDescBoxLarge.visible = false;
						//trace("ROLLOUT EFFECTBOX");
						break;	
					case "click":
						//trace("CLICK EFFECTBOX");
						break;	
				}
			}
		}
		
		public function dispose():void {
		}
	}
}